package com.example.yunpicture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yunpicture.annotation.AuthCheck;
import com.example.yunpicture.common.BaseResponse;
import com.example.yunpicture.common.DeleteRequest;
import com.example.yunpicture.common.ResultUtils;
import com.example.yunpicture.constant.UserConstant;
import com.example.yunpicture.exception.ErrorCode;
import com.example.yunpicture.exception.ThrowUtils;
import com.example.yunpicture.model.dto.user.*;
import com.example.yunpicture.model.entity.User;
import com.example.yunpicture.model.vo.LoginUserVO;
import com.example.yunpicture.model.vo.UserVO;
import com.example.yunpicture.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    //@ApiOperation("注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwif(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    //@ApiOperation("登录")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwif(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        LoginUserVO userVo = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(userVo);
    }

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    //@ApiOperation("获取当前登录用户")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @PostMapping("/loginout")
    //@ApiOperation("退出登录")
    public BaseResponse<Boolean> userLoginout(HttpServletRequest request) {
        ThrowUtils.throwif(request == null, ErrorCode.PARAMS_ERROR);
        boolean result = userService.userLoginout(request);
        return ResultUtils.success(result);
    }


    /**
     * （管理员）创建用户
     *
     * @param userAddRequest
     * @return
     */
    @PostMapping("/add")
    //@ApiOperation("创建用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> userRegister(@RequestBody UserAddRequest userAddRequest) {
        ThrowUtils.throwif(userAddRequest == null, ErrorCode.PARAMS_ERROR);
        User user = new User();
        BeanUtils.copyProperties(userAddRequest, user);
        //设置用户初始密码
        String password = userService.getEncryptPassword("12345678");
        user.setUserPassword(password);
        //保存到数据库
        boolean result = userService.save(user);
        ThrowUtils.throwif(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(user.getId());
    }

    /**
     * (管理员)根据id获取用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    //@ApiOperation("(管理员)根据id获取用户信息")
    public BaseResponse<User> getUserById(Long id) {
        ThrowUtils.throwif(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwif(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * (所有人)根据id获取用户脱敏信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    //@ApiOperation("(所有人)根据id获取用户脱敏信息")
    public BaseResponse<UserVO> getUserVOById(Long id) {
        ThrowUtils.throwif(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwif(user == null, ErrorCode.NOT_FOUND_ERROR);
        UserVO userVo = userService.getUserVO(user);
        return ResultUtils.success(userVo);
    }

    /**
     * (管理员)根据id删除用户
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    //@ApiOperation("(管理员)根据id删除用户")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUserById(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwif(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean result = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * (管理员)根据id修改用户信息
     *
     * @param updateRequest
     * @return
     */
    @PostMapping("/update")
    //@ApiOperation("(管理员)根据id修改用户信息")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUserById(@RequestBody UserUpdateRequest updateRequest) {
        ThrowUtils.throwif(updateRequest == null || updateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        User user = new User();
        BeanUtils.copyProperties(updateRequest, user);
        boolean result = userService.updateById(user);
        ThrowUtils.throwif(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * (管理员)查询用户信息列表
     *
     * @param userQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    //@ApiOperation("(管理员)查询用户信息列表")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> listUserVoByPage(@RequestBody UserQueryRequest userQueryRequest) {
        ThrowUtils.throwif(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current = userQueryRequest.getCurrent();
        long pageSize = userQueryRequest.getPageSize();
        Page<User> userPage = userService.page(new Page<>(current, pageSize), userService.getQueryWrapper(userQueryRequest));
        Page<UserVO> userVoPage = new Page<>(current, pageSize, userPage.getTotal());
        List<UserVO> userVoList = userService.getUserVOList(userPage.getRecords());
        userVoPage.setRecords(userVoList);
        return ResultUtils.success(userVoPage);
    }
}
